import 'dart:developer';

import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';

class DBConfig{
  static late Database _database;
  static const int _version=1;
  static const String _databaseName="development_assistant.db";

  static Future<void> initDB() async{
    log("数据库初始化",time: DateTime.now());
    String path=join(await getDatabasesPath(),_databaseName);
    _database=await openDatabase(
        path, version: _version,
        onCreate: (db,version) {
          db.execute(
            "create table server_info("
                "server_id   integer constraint sever_pk primary key autoincrement,"
                "server_type text,"
                "server_name text,"
                "server_host text,"
                "server_port text,"
                "server_url  text);"
          );

          db.execute(
              "create table docker_info("
                  " docker_id   integer constraint docker_pk primary key autoincrement,"
                  "docker_name text,"
                  "docker_cid  text,"
                  "docker_tag  text,"
                  "docker_file text,"
                  "docker_from text);"
          );
          
          db.execute(
              "create table db_info("
                  "db_id   integer constraint db_info_pk primary key autoincrement,"
                  "db_name text,"
                  "db_desc text,"
                  "db_host text,"
                  "db_port text,"
                  "db_url  text);"
          );
          
          db.execute(
              "create table table_info("
                  "table_id integer constraint table_info_pk primary key autoincrement,"
                  "table_name text,"
                  "table_sort text,"
                  "table_engine text,"
                  "db_id integer);"
          );

          db.execute(""
              "create table column_info("
              "column_id   integer constraint column_info_pk primary key autoincrement"
              ",column_name text,"
              "column_type text,"
              "column_kind text,"
              "table_id integer);"
          );

          db.execute(""
              "create table api_info("
              "api_id  integer constraint api_pk primary key autoincrement,"
              "api_name text,"
              "api_method text,"
              "api_url  text,"
              "api_date text);"
          );

          db.execute("create table test_api("
              "test_id integer constraint auto_test_api_auto_test_test_id_fk references auto_test,"
              "api_name   text,"
              "api_method text,"
              "api_url    text);"
          );

          db.execute("create table auto_test("
              "test_id  integer constraint auto_test_pk primary key,"
              "test_name   text,"
              "test_env   text,"
              "test_thread  integer,"
              "test_interval integer,"
              "test_cycles   integer,"
              "test_success  integer,"
              "test_error    integer,"
              "request_time  text,"
              "average_time  text,"
              "test_date text);"
              );

          db.execute("create table env_type("
              "env_id  integer constraint auto_test_pk primary key,"
              "env_name   text,"
              "env_url text);"
          );

          db.insert("env_type",{
            'env_id':1,
            'env_name':'测试环境',
            'env_url':''
          });

          db.insert("env_type",{
            'env_id':2,
            'env_name':'正式环境',
            'env_url':''
          });

          db.insert("env_type",{
            'env_id':3,
            'env_name':'体验环境',
            'env_url':''
          });

          db.insert("env_type",{
            'env_id':4,
            'env_name':'云端环境',
            'env_url':''
          });

        }
    );

  }

  static getDatabase(){
    return _database;
  }

}